package com.gosophia.util;

import org.springframework.security.context.SecurityContextHolder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.gosophia.reportData.dto.LaunchReportJobDTO;
import com.gosophia.reportData.entity.LaunchReportJob;
import com.gosophia.reportData.ws.LaunchReportWebService;
import com.gosophia.reportData.ws.ReportJobWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.runqian.report4.usermodel.input.AbstractInputListener;

public class FormReportInputListener extends AbstractInputListener {

	private Long jobId;

	private ReportJobWebService reportJobWebService;
	private LaunchReportWebService launchReportWebService;

	public FormReportInputListener() {
	}

	public void getReportJobWebService() {
		if (reportJobWebService == null) {
			WebApplicationContext ctx = WebApplicationContextUtils
					.getWebApplicationContext(OAContextLoaderListener.servletContext);
			reportJobWebService = (ReportJobWebService) ctx
					.getBean("reportJobWebServiceImpl");
			launchReportWebService = (LaunchReportWebService) ctx
					.getBean("launchReportWebServiceImpl");
		}
	}

	@Override
	public void afterSave() throws Exception {
		getReportJobWebService();
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		String description="";
		if (this.getInputSql().length > 0
				&& this.getInputSql()[0].getParams() != null
				&& this.getInputSql()[0].getParams().length != 0) {
			jobId = Long.parseLong(this.getInputSql()[0].getOtherColValuesMap()
					.get("REPORTID").toString());
			 description = this.getInputSql()[0].getOtherColValuesMap()
					.get("REPORTDESCRIPTION").toString();
		}

		LaunchReportJobDTO launchReportJob = launchReportWebService.findFirstJobByJobId(jobId);
		reportJobWebService.excelSubmitted(jobId, launchReportJob
				.getJobDataId(), userInfoId, description);
	}


}
